//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Loss.h"
using namespace Cephei::QL::Experimental::Credit;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Experimental::Credit::CLoss::CLoss (Microsoft::FSharp::Core::FSharpOption<Double>^ t, Microsoft::FSharp::Core::FSharpOption<Double>^ a) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phLoss = NULL;
#endif
        QuantLib::Real _t = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (t) ? (QuantLib::Real)ValueHelper::Convert (t->Value) : 0.0); //4
        QuantLib::Real _a = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (a) ? (QuantLib::Real)ValueHelper::Convert (a->Value) : 0.0); //4
        _ppLoss = new boost::shared_ptr<QuantLib::Loss> (new QuantLib::Loss ( _t,  _a ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::CLoss::CLoss (boost::shared_ptr<QuantLib::Loss>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLoss = NULL;
#endif
	_ppLoss = &childNative;
    
}
Cephei::QL::Experimental::Credit::CLoss::CLoss (QuantLib::Loss& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLoss = NULL;
#endif
	_ppLoss = new boost::shared_ptr<QuantLib::Loss> (&childNative);
    
    _LossOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CLoss::CLoss (CLoss^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLoss = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLoss = new boost::shared_ptr<QuantLib::Loss> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CLoss::CLoss (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLoss = NULL;
#endif
	if (!t->IsSubclassOf(CLoss::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CLoss::CLoss (QuantLib::Handle<QuantLib::Loss>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLoss = &childNative;
	_ppLoss = &static_cast<boost::shared_ptr<QuantLib::Loss>>(childNative.currentLink());
    
    _LossOwner = owner;
}
Cephei::QL::Experimental::Credit::CLoss::CLoss (QuantLib::Handle<QuantLib::Loss> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLoss = &childNative;
	_ppLoss = &static_cast<boost::shared_ptr<QuantLib::Loss>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CLoss::CLoss (QuantLib::Loss childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLoss = NULL;
#endif
	_ppLoss = new boost::shared_ptr<QuantLib::Loss> (new QuantLib::Loss (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CLoss::~CLoss ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLoss != NULL)
    {
	    delete _ppLoss;
        _ppLoss = NULL;
    }
}
Cephei::QL::Experimental::Credit::CLoss::!CLoss ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLoss != NULL)
    {
	    delete _ppLoss;
    }
}
QuantLib::Loss& Cephei::QL::Experimental::Credit::CLoss::GetReference ()
{
    if (_ppLoss == NULL) throw REFNEW NativeNullException ();
	return **_ppLoss;
}
boost::shared_ptr<QuantLib::Loss>& Cephei::QL::Experimental::Credit::CLoss::GetShared ()
{
    if (_ppLoss == NULL) throw REFNEW NativeNullException ();
	return *_ppLoss;
}
QuantLib::Loss* Cephei::QL::Experimental::Credit::CLoss::GetPointer ()
{
    if (_ppLoss == NULL) throw REFNEW NativeNullException ();
	return &**_ppLoss;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Loss>& Cephei::QL::Experimental::Credit::CLoss::GetHandle ()
{
	if (_phLoss == NULL)
	{
		_phLoss = new Handle<QuantLib::Loss> (*_ppLoss);
	}
	return *_phLoss;
}
#endif
bool Cephei::QL::Experimental::Credit::CLoss::HasNative () 
{
	return (_ppLoss != NULL);
}

Cephei::QL::Experimental::Credit::CLossVector::CLossVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Credit::CLossVector::~CLossVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Experimental::Credit::CLossVector::!CLossVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CLossVector::CLossVector (std::vector<QuantLib::Loss>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Experimental::Credit::CLossVector::CLossVector (std::vector<QuantLib::Loss> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::Loss> (vec);
}
#endif
Cephei::QL::Experimental::Credit::CLossVector::CLossVector (std::vector<boost::shared_ptr<QuantLib::Loss>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CLossVector::CLossVector (std::vector<QuantLib::Handle<QuantLib::Loss>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Experimental::Credit::CLossVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Experimental::Credit::ILoss^ Cephei::QL::Experimental::Credit::CLossVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::Loss& native = (*_pVValue)[index];
		return REFNEW CLoss (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::Loss& native = *(*_pVBoost)[index];
		return REFNEW CLoss (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::Loss& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CLoss (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Experimental::Credit::CLossVector::default::set (int index, Cephei::QL::Experimental::Credit::ILoss^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CLoss^>(value)->GetReference ());
		}
        else
        {
            QuantLib::Loss& c = (*_pVValue)[index];
            c = dynamic_cast<CLoss^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CLoss^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::Loss>& c = (*_pVBoost)[index];
		c = dynamic_cast<CLoss^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::Loss> ());
		}
		QuantLib::Handle<QuantLib::Loss>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::Loss>& b = static_cast<boost::shared_ptr<QuantLib::Loss>>(c.currentLink());
		b = dynamic_cast<CLoss^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^ Cephei::QL::Experimental::Credit::CLossVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<ILoss^> (dynamic_cast<ILoss^>(default[index]));
}

void Cephei::QL::Experimental::Credit::CLossVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^ value) 
{
	default[index] = safe_cast<ILoss^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::Loss>& Cephei::QL::Experimental::Credit::CLossVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::Loss>>& Cephei::QL::Experimental::Credit::CLossVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::Loss>>& Cephei::QL::Experimental::Credit::CLossVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Experimental::Credit::CLossVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::Loss> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::Loss>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::Loss>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::ILoss^ Cephei::QL::Experimental::Credit::CLoss_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ t, Microsoft::FSharp::Core::FSharpOption<Double>^ a)
{
    return REFNEW CLoss ( t,  a);
}
/* <summary>Create a single dimension array of Loss that can be passed into other function </summary> */
IVector<Cephei::QL::Experimental::Credit::ILoss^>^ Cephei::QL::Experimental::Credit::CLoss_Factory::CreateVector ()
 {
	 return REFNEW CoVector<ILoss^> (REFNEW CLossVector ());
 }
IVector<Cephei::QL::Experimental::Credit::ILoss^>^ Cephei::QL::Experimental::Credit::CLoss_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<ILoss^>^ source)
 {
	 return REFNEW CoVector<ILoss^> (source, REFNEW CLossVector ());
 }
/* <summary>Create a single dimension array of Loss with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^>^>^ Cephei::QL::Experimental::Credit::CLoss_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^>^> (REFNEW CoCellVector<Cephei::QL::Experimental::Credit::ILoss^> (REFNEW CLossVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^>^>^ Cephei::QL::Experimental::Credit::CLoss_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Experimental::Credit::ILoss^>^>^> (REFNEW CoCellVector<Cephei::QL::Experimental::Credit::ILoss^> (source, REFNEW CLossVector ()));
 }

